package com.morningstar.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.morningstar.common.dao.entity.CommonQuery;
import com.morningstar.common.result.Result;
import com.morningstar.common.result.ServiceCheckResult;
import com.morningstar.system.dao.entity.SysDictType;
import com.morningstar.system.service.SysDictTypeService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import static com.morningstar.common.constants.permission.PermissionConstants.*;

/**
 * 字典类型接口
 *
 * @author yanglianshan
 */
@Slf4j
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController {

    private static final String PERMISSION = "system.dict.type";
    private static final String PERMISSION_LIST = PERMISSION + LIST;
    private static final String PERMISSION_DETAIL = PERMISSION + DETAIL;
    private static final String PERMISSION_UPDATE = PERMISSION + UPDATE;
    private static final String PERMISSION_ADD = PERMISSION + ADD;
    private static final String PERMISSION_DELETE = PERMISSION + DELETE;

    @Resource
    private SysDictTypeService dictTypeService;

    /**
     * 字典类型列表
     *
     * @param commonQuery 通用查询类
     * @return IPage<SysDictType> 字典列表
     */
    @PostMapping(value = "/list")
    @SaCheckPermission(PERMISSION_LIST)
    public Result<IPage<SysDictType>> list(@RequestBody CommonQuery<SysDictType, SysDictType> commonQuery) {
        SysDictType sysDictType = commonQuery.getQuery();
        LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<>();
        if (sysDictType != null) {
            queryWrapper.like(StringUtils.isNotBlank(sysDictType.getDictName()), SysDictType::getDictName, sysDictType.getDictName());
            queryWrapper.like(StringUtils.isNotBlank(sysDictType.getDictType()), SysDictType::getDictType, sysDictType.getDictType());
        }
        queryWrapper.orderByDesc(SysDictType::getCreateTime);
        queryWrapper.orderByDesc(SysDictType::getDictId);
        return Result.successData(dictTypeService.page(commonQuery.getPage(), queryWrapper));
    }

    /**
     * 新增字典类型
     *
     * @param sysDictType 字典类型实体类
     * @return 基本结果类
     */
    @PostMapping
    @SaCheckPermission(PERMISSION_ADD)
    public Result<Object> add(@RequestBody SysDictType sysDictType) {
        ServiceCheckResult checkResult = dictTypeService.checkAddAndUpdate(sysDictType);
        if (checkResult.isResult()) {
            return Result.fail(checkResult.getMessage());
        }
        dictTypeService.save(sysDictType);
        return Result.successNoData();
    }

    /**
     * 更新字典类型
     *
     * @param sysDictType 字典类型实体类
     * @return 基本结果类
     */
    @PostMapping("/update")
    @SaCheckPermission(PERMISSION_UPDATE)
    public Result<Object> update(@RequestBody SysDictType sysDictType) {
        ServiceCheckResult checkResult = dictTypeService.checkAddAndUpdate(sysDictType);
        if (checkResult.isResult()) {
            return Result.fail(checkResult.getMessage());
        }
        dictTypeService.updateById(sysDictType);
        return Result.successNoData();
    }

    /**
     * 删除字典类型
     *
     * @param dictId 字典类型Id
     * @return 基本结果类
     */
    @GetMapping("/delete")
    @SaCheckPermission(PERMISSION_DELETE)
    public Result<Object> delete(@RequestParam("dictId") Long dictId) {
        dictTypeService.removeById(dictId);
        return Result.successNoData();
    }

    /**
     * 字典类型详细
     *
     * @param dictId 字典类型Id
     * @return 字典类型详细
     */
    @GetMapping("/detail")
    @SaCheckPermission(PERMISSION_DETAIL)
    public Result<SysDictType> detail(@RequestParam("dictId") Long dictId) {
        return Result.successData(dictTypeService.getById(dictId));
    }
}
